// pages/sales/boss_ai/custom_list.js
var cf = require("../../../config.js");
var util = require("../../../utils/util.js");
var cusmallToken = wx.getStorageSync('cusmallToken');
var baseHandle = require("../../template/baseHandle.js");
var app = getApp();
Page(Object.assign({}, baseHandle,{

  /**
   * 页面的初始数据
   */
  data: {
    extConfig: wx.getExtConfigSync ? wx.getExtConfigSync() : {},
    app: app,
    staticResPath: cf.config.staticResPath,
    userImagePath: cf.config.userImagePath,
    list: [],
    isLoading: false,
    pageSize: 15,
    curPage: 1,
    total: 0,
    isBottom: false,
    relationMap:{},
    sourceArr: ["最后扫描", "最后分享", "最后搜索", "最后交接"]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var ctx = this;
    ctx.setData({
      eId:options.eId
    })
    app.getUserInfo(this, options, function (userInfo, res) {
      cusmallToken = wx.getStorageSync('cusmallToken');
      ctx.findByEmployeeOpenId();
    });
  },
  findByEmployeeOpenId() {
    var ctx = this;
    if (ctx.data.isLoading || ctx.data.isBottom) {
      return;
    }
    ctx.setData({ isLoading: true });
    wx.request({
      url: cf.config.pageDomain + "/applet/mobile/visitcard/memberrelation/findByEmployeeOpenId",
      data: {
        eid:ctx.data.eId,
        cusmallToken: cusmallToken,
        nickName: "",
        start: (ctx.data.curPage - 1) * ctx.data.pageSize,
        limit: ctx.data.pageSize,
        source: ""
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var data = res.data;
        if (data && 0 === data.ret) {

          if (data.model.relation) {
            for (let item of data.model.relation) {
              item.followSatus = (item.followTime && ctx.followTimeToTxt(item.followTime)) || "未跟进客户";
              ctx.setData({
                ['relationMap.' + item.memberId + '']: item
              });
            }

          } else {
            ctx.setData({
              relationMap: null
            })
          }
          var list = data.model.list || [];
          for (let item of list) {
            item.behaviorTime = util.formatTime(new Date(ctx.data.relationMap[item.id].createTime));
          }
          ctx.setData({
            list: ctx.data.list.concat(list),
            total: data.model.total
          });
          if (data.model.total == ctx.data.list.length) {
            ctx.setData({
              isBottom: true
            });
          }
          
        } else {
          wx.showToast({
            title: data.msg,
            icon: "none"
          })
        }
      },
      fail() {

      },
      complete() {
        ctx.setData({ isLoading: false });
      }
    });
  },
  getSource(src) {
    switch (src) {
      case 0:
        return "最后扫描";
      case 1:
        return "最后分享";
      case 2:
        return "最后搜索";
      case 3:
        return "最后交接";
      default:
        return "";
    }
  },
  gotoDetail: function (e) {
    var mid = e.currentTarget.dataset.mid;
    var src = e.currentTarget.dataset.src;
    var openid = e.currentTarget.dataset.openid;
    var eopenid = e.currentTarget.dataset.eopenid;
    var eId = e.currentTarget.dataset.eid;
    wx.navigateTo({
      url: "/pages/sales/custom_detail/index?" + "&openid=" + openid + "&src=" + src + "&eId=" + eId + "&hideOp=1&eOpenId=" + eopenid,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let ctx = this;
    if (ctx.data.isLoading) {
      return;
    }
    ctx.setData({ curPage: ctx.data.curPage + 1 });
    ctx.findByEmployeeOpenId();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
}));